<?php
/*
 *  supplier 供货商添加类
 *
 *  @param $c 一般是类名
 *  @param $a 一般是方法名
 */

class order extends Admin {

    private $tab = array( 'orderMan' => '添加订单','myOrder' => '我的订单');

    private $orderStatus = array('已创建','已发货','已退货','已换货','缺货关闭','缺货流转');

    private $color = array('深红', '粉红','橘红','橘黄 ',
        '玫瑰红','橙色','深黄','淡黄','草绿',
        '墨绿', '柠檬黄', '黄绿', '蓝绿', '浅绿',
        '天蓝', '湖蓝', '黑色', '白色', '紫色',
        '红灰', '黄灰', '蓝灰', '紫灰', '浅灰');
    private $size = array('080CM','085CM','090CM','095CM','100CM',"105CM","110CM",
        "115CM","120CM","125CM","130CM","135CM","140CM","145CM","150CM","155CM","160CM");


    private $area = array('全中国','北京市','天津市','河北省','山西省','内蒙古',             //华北
        '辽宁省','吉林省','黑龙江',                                           //东北
        '上海市','江苏省','浙江省','安徽省','福建省','江西省','山东省',    //华东
        '河南省','湖北省','湖南省','广东省','广西省','海南省',              //华中
        '重庆市','四川省','贵州省','云南省','西藏省',                        //西南
        '陕西省','甘肃省','青海省','宁夏','新疆',                            //西北
        '香港','澳门','台湾');                                                 //特区


    public function __construct() {
        parent::__construct();
        $this->type = G( 'a' ) == 'index' ? '' : G( 'a' );
        $this->pk = "orderNo";
    }

    public function index( $c, $a ) {
        $type = G( 'type', 'orderMan' );
        $method = "{$a}_{$type}";
        $this->$method( $c, $a, $type );
    }

    private function index_orderMan( $c, $a, $type ) {
        $type = G( 'type', 'orderMan' );

    //    $this->table = 'order';


        $sql = "select A.*,B.goodsName from xbl_orderRoam as A, xbl_order as B where A.orderNo = B.orderNo and A.orderStatus = 0 ";   //不是admin用户需要加上用户ID的判断。
        $result = $this->Paged()->findSql($sql);

        $pager = $result['page'];
        $result = $result['result'];

        foreach($result as $key => $val)
        {
            $result[$key]['orderStatus'] =  $this->orderStatus[$val['orderStatus']] ;
        }


        include $this->display( $c );

    }

    private function index_myOrder( $c, $a, $type ) {
        //$sql = "select B.goodsName,B.orderNo,C.dateCreate,C.orderStatus from xbl_orderRoam as C, xbl_order as B,xbl_employeeOrder as A where A.orderNo = B.orderNo and  A.orderNo = C.orderNo and A.status = 0 ";   //不是admin用户需要加上用户ID的判断。
       $sql = "select B.goodsName,B.goodsNo,B.taobaoOrderNo,B.color,B.size,C.orderStatus from xbl_orderRoam as C, xbl_order as B,xbl_employeeOrder as A where A.orderNo = B.orderNo and  A.orderNo = C.orderNo and A.status = 0 ";   //不是admin用户需要加上用户ID的判断。

        if($this->admin['rights']  != 0 )
            $sql = $sql." and A.employeeId = ".$this->admin['id'];

        $result = $this->Paged()->findSql($sql);

        $pager = $result['page'];
        $result = $result['result'];

      //  print_r($result);



        foreach($result as $key => $val)
        {
            $result[$key]['orderStatus'] =  $this->orderStatus[$val['orderStatus']] ;
            $result[$key]['color'] = $this->color[$val['color']];
            $result[$key]['size'] = $this->size[$val['size']];
        }





        include $this->display( $c );
    }


    public function addOrder( $c, $a, $type ) {


        if(G('goodsNo'))
        {
            $this->table = 'stockInfo';

            $sql = "select distinct color from xbl_stockInfo where goodsNo = ".G('goodsNo');
            $color = $this->findSql($sql);

            $sql = "select distinct size from xbl_stockInfo where goodsNo = ".G('goodsNo');
            $size = $this->findSql($sql);

            $this->table = 'bsInfo';
            $this->pk = 'goodsNo';
            $result =  $this->find(array('goodsNo'=>G('goodsNo')),null,'goodsNo,materialNo,goodsName');
        }

        $result['editMode'] = 0;

        include $this->display( $c );
    }



    public function del($c, $a )
    {
        $this->table = 'order';
        $this->delete(array('orderNo'=>G('orderNo')));
        $this->table = 'orderRoam';
        $this->delete(array('orderNo'=>G('orderNo')));
        $this->table = 'employeeOrder';
        $this->delete(array('orderNo'=>G('orderNo')));
        $type = G( 'type', 'orderMan' );

        header("location:?c=order");
        //include $this->display($c,'index');
    }

    public function postGoods( $c, $a, $type ) {
        $result['orderNo'] = G( 'orderNo') ;

        $this->table =  'logistic';
        $this->pk = 'companyNo';
        $res = $this->findAll(null,null,'companyNo,companyName');

      //  print_r($res);

        include $this->display( $c );
    }
    public function evaluateGoods( $c, $a, $type ) {
        $result['orderNo'] = G( 'orderNo') ;
        include $this->display( $c );
    }
    public function backGoods( $c, $a, $type ) {
        $result['orderNo'] = G( 'orderNo') ;
        include $this->display( $c );
    }
    public function cancelGoods( $c, $a, $type ) {
        $result['orderNo'] = G( 'orderNo') ;
        include $this->display( $c );
    }
    public function editOrder( $c, $a) {

        $this->table = 'order';
        $condition = array('orderNo'=>  G('orderNo'));
        $result = $this->find($condition);
        $result['editMode'] = 1;


        $this->table = 'stockInfo';

        $sql = "select distinct color from xbl_stockInfo where goodsNo = ".$result['goodsNo'];
        $color = $this->findSql($sql);

        $sql = "select distinct size from xbl_stockInfo where goodsNo = ".$result['goodsNo'];
        $size = $this->findSql($sql);


     //   print_r($result);




        include $this->display( $c );
    }


    public function edit( $c, $a ) {
        if ( !$id = intval( G( 'id' ) ) ) {
            $this->error();
            exit;
        }
        $this->table = ''; // 数据库表名
        $result = $this->find( array( 'id' => $id ) ); // 查找数据
        include $this->display( $c ); // 加载模板
    }

    public function add( $c, $a ) {
        include $this->display( $c );
    }

    public function saveEvaluate() {
        # 获取表单数据
        foreach ( $_POST as $key => $val ) {
            $rows[ $key ] = htmlspecialchars( trim( $val ) );
        }

        /**zouhong-edit**/
        $rows['evaluateDate'] = date("YmdHis");
        /**zouhong-edit-end**/

        /***评价处理***/
        # 注意 表单字段必须跟数据库表字段名一样
        $this->table = 'evaluate'; // 数据库表名
        $condition = array('orderNo'=> $_POST['orderNo']);
        $result = $this->find($condition, 'orderNo');
        if (  $result ) {
            $msg = $this->update( array( 'id' => $result['id'] ), $rows ) ? 'success' : 'failed'; //更新订单
            $id = $result['id'];
        } else {
            $msg = $this->create( $rows ) ? 'success' : 'failed'; // 添加订单
            $res = $this->find(null, null, "max(id)");
            $id = intval($res['max(id)']);
        }

        /***商品订单流转表处理***/
        unset($rows);
        $rows['evalueateId'] = $id;
        $rows['orderStatus'] = 3;    /*'3' means 已评价*/
        $condition = array('orderNo'=>$_POST['orderNo']);
        $this->table = 'orderRoam';
        $msg = $this->update($condition, $rows ) ? 'success' : 'failed'; //更新商品流转表

        $this->table = 'order';
        $msg = $this->update($condition, $rows ) ? 'success' : 'failed'; //更新商品流转表

        exit( $msg );
    }

    public function savePost() {
        # 获取表单数据
        foreach ( $_POST as $key => $val ) {
            $rows[ $key ] = htmlspecialchars( trim( $val ) );
        }

        /**zouhong-edit**/
        $rows['postDate'] = date("YmdHis");
        $rows['postPersonId'] = $this->admin['id'];
        /**zouhong-edit-end**/

        /***评价处理***/
        # 注意 表单字段必须跟数据库表字段名一样
        $this->table = 'postGoods'; // 数据库表名
        $condition = array('orderNo'=> $_POST['orderNo']);
        $result = $this->find($condition, 'goodsNo');
        if ( $result ) {
            $msg = $this->update( array( 'id' => $result['id'] ), $rows ) ? 'success' : 'failed'; //更新订单
            $id = $result['id'];
        } else {
            $id = $this->create( $rows ) ; // 添加订单
        }

        /***商品订单流转表处理***/

        unset($rows);
        $rows['postId'] = $id;
        $rows['orderStatus'] = 1;    /*'1' means 已发货*/
        $condition = array('orderNo'=>$_POST['orderNo']);
        $rows['datePost'] = date("Y.m.d");
        $this->table = 'orderRoam';
        $msg = $this->update($condition, $rows ) ? 'success' : 'failed'; //更新商品流转表

        unset($rows);
        $rows['status'] = 1;
        $this->table = 'employeeOrder';
        $msg = $this->update($condition, $rows ) ? 'success' : 'failed'; //更新商品流转表

        $this->table = 'orderRoam';
        $res = $this->find($condition,null,'orderCreateId');


        $rows['cutomId'] = $res['orderCreateId'];
        $rows['orderNo'] = $_POST['orderNo'];
        $rows['status'] = 0;
        $this->table = 'employeeOrder';
        $id = $this->create( $rows ) ;

        exit( $msg );
    }

    public function saveBack() {
        # 获取表单数据
        foreach ( $_POST as $key => $val ) {
            $rows[ $key ] = htmlspecialchars( trim( $val ) );
        }

        /**zouhong-edit**/
        $rows['cancelDate'] = date("YmdHis");
        /**zouhong-edit-end**/

        /***换货记录***/
        # 注意 表单字段必须跟数据库表字段名一样
        $this->table = 'exchangeOrder'; // 数据库表名
        $condition = array('orderNo'=> $_POST['orderNo']);
        $result = $this->find($condition, 'exchangeOrderNo');
        if (  $result ) {
            $msg = $this->update( array( 'exchangeOrderNo' => $result['exchangeOrderNo'] ), $rows ) ? 'success' : 'failed'; //更新订单
            $id = $result['exchangeOrderNo'];
        } else {
            $msg = $this->create( $rows ) ? 'success' : 'failed'; // 添加订单
            $res = $this->find(null, null, "max(exchangeOrderNo)");
            $id = intval($res['max(exchangeOrderNo)']);
        }

        /***商品订单流转表处理***/
        unset($rows);
        $rows['exchangeId'] = $id;
        $rows['orderStatus'] = 2;    /*'2' means 换货终止订单*/
        $condition = array('orderNo'=>$_POST['orderNo']);
        $this->table = 'orderRoam';
        $msg = $this->update($condition, $rows ) ? 'success' : 'failed'; //更新顾客订单表

        $this->table = 'order';
        $msg = $this->update($condition, $rows ) ? 'success' : 'failed'; //更新商品流转表

        exit( $msg );
    }
    public function saveCancel() {
        # 获取表单数据
        foreach ( $_POST as $key => $val ) {
            $rows[ $key ] = htmlspecialchars( trim( $val ) );
        }

        /**zouhong-edit**/
        $rows['cancelDate'] =date("YmdHis");
        /**zouhong-edit-end**/

        /***评价处理***/
        # 注意 表单字段必须跟数据库表字段名一样
        $this->table = 'cancelOrder'; // 数据库表名
        $condition = array('orderNo'=> $_POST['orderNo']);
        $result = $this->find($condition, 'cancelOrderNo');
        if (  $result ) {
            $msg = $this->update( array( 'cancelOrderNo' => $result['cancelOrderNo'] ), $rows ) ? 'success' : 'failed'; //更新订单
            $id = $result['cancelOrderNo'];
        } else {
            $msg = $this->create( $rows ) ? 'success' : 'failed'; // 添加订单
            $res = $this->find(null, null, "max(cancelOrderNo)");
            $id = intval($res['max(cancelOrderNo)']);
        }

        /***商品订单流转表处理***/

        unset($rows);
        $rows['cancelOrderId'] = $id;
        $rows['orderStatus'] = 4;    /*'4' means 退货*/
        $condition = array('orderNo'=>$_POST['orderNo']);
        $this->table = 'orderRoam';
        $msg = $this->update($condition, $rows ) ? 'success' : 'failed'; //更新商品流转表

        $this->table = 'order';
        $msg = $this->update($condition, $rows ) ? 'success' : 'failed'; //更新商品流转表

        exit( $msg );
    }


    public function save() {
        # 获取表单数据


        foreach ( $_POST as $key => $val ) {
            $rows[ $key ] = htmlspecialchars( trim( $val ) );
        }

        /***订单表处理***/
        # 注意 表单字段必须跟数据库表字段名一样
        $this->table = 'order'; // 数据库表名


        if($_POST['editMode'] == 0)
        {
            $id = $this->create( $rows ) ; // 添加订单

            print_r("9900");

            /***商品订单流转表处理***/
            unset($rows);
            $rows['orderNo'] = $id;
            $rows['taobaoOrderNo'] = $_POST['taobaoOrderNo'];
            $rows['orderStatus'] = 0;   //'1':已创建
            $rows['orderCreateId'] = $this->admin['id'];
            $rows['dateCreate'] = date("Y-M-D");


            $this->table = 'orderRoam';

            $msg = $this->create($rows)?'success':'failed';
            print_r("9900");
            /***订单表处理***/

            $cusAddr = $_POST['cusAddr'];
            $this->table = 'stockPos';
            $this->pk = 'stockNo';
            $res = $this->findAll(null,null,'area,personId');
            print_r($res);

            $this->table = 'employeeOrder';

            /**向管理员帐户添加处理信息**/

            $rows['customId'] = 0 ;
            $rows['orderNo'] = $id;
            $rows['status'] = 0;
            $msg = $this->create($rows);//? 'success':'failed';

            /**向有权限用户添加订单处理信息**/

            foreach($res as $key =>$val)
            {
                print_r("start");
                $personId = 10000;
                $area = '';

                if($val['area'])
                {
                    if(strstr($val['area'],','))
                         $area = explode(',',$val['area']);
                    print_r("start2");
                    print_r($area);
                    if($area)
                    {
                        print_r("start3");
                        print_r($cusAddr);
                        print_r($area);
                        if(in_array($cusAddr,$area))
                        {
                            print_r("start3");
                            $personId = $val['personId'];
                            print_r("555");
                        }

                    }/*else if($cusAddr == intval($area,10))
                    {
                        print_r("start4");
                        $personId = $val['personId'];
                        print_r("666");
                    }*/
                    else
                    {
                        print_r("start7");
                        $personId =10000;
                    }



                    print_r("personId:$personId");
                    print_r($area);


                    if(10000 != $personId)
                    {
                        /**员工订单表处理**/
                        print_r("177");
                        $rows['customId'] = $personId ;
                        $rows['orderNo'] = $id;
                        $rows['status'] = 0;
                        $msg = $this->create($rows);//? 'success':'failed';
                    }
                }


            }


        }
        else
        {
            /***/
            print_r($rows);
            $msg = $this->update(array('orderNo'=>$rows['orderNo']),$rows ) ; // 添加订单
        }


        exit( $msg );

    }

    public function search_order( $c, $a ) {

        $sql = "select A.*,B.goodsName from xbl_orderRoam as A, xbl_order as B where A.orderNo = B.orderNo and A.orderStatus = 0 ";
        if($this->admin['rights']  != 0 )
            $sql = $sql." and A.employeeId = ".$this->admin['id'];

        $sql = $sql."and B.taobaoOrderNo = ".G('key');

        print_r($sql);

        $result = $this->Paged()->findSql($sql);
        $pager = $result['page'];
        $result = $result['result'];

        $type = G( 'type', 'orderMan' );
        include $this->display( $c ); // 加载模板

    }

    public function search_myorder( $c, $a ) {

        $type = G( 'type', 'myOrder' );

        $sql = "select B.goodsName,B.goodsNo,B.orderNo,B.color,B.size,C.orderStatus from xbl_orderRoam as C, xbl_order as B,xbl_employeeOrder as A where A.orderNo = B.orderNo and  A.orderNo = C.orderNo and A.status = 0 ";   //不是admin用户需要加上用户ID的判断。

        if($this->admin['rights']  != 0 )
            $sql = $sql." and A.employeeId = ".$this->admin['id'];

        $sql = $sql." and B.taobaoOrderNo = ".G('key');

        print_r($sql);

        $result = $this->Paged()->findSql($sql);

        $pager = $result['page'];
        $result = $result['result'];

        include $this->display( $c ); // 加载模板

    }
}